<?php
/**
 * User: 魔十七
 * Date: 2020/11/3
 * Time: 17:47
 */

namespace Admin\Controller;
use Think\Controller;
class CourseController extends JudgeLoginController
{
    /**
     * 课程类型
     */
    public function index(){
        $this->display();
    }

    //获取数据
    public function getList(){
        $freeride = M('course');
        $data = $freeride->select();
        if (!$data) {
            $data = array(
                "code" => 1,
                "msg" => "未找到",
            );
        }else{
            $data = array(
                "code" => 0,
                "msg" => "",
                "count" => 8,
                "data" =>$data,
            );
        }
        $this->ajaxReturn($data);
    }

    //增加
    public function add(){
        $param=$_GET;
        $data = M('course')->add($param);
        if (!$data) {
            $arr = array(
                "code" => 404,
                "msg" => "添加失败",
            );
        }else{
            $arr = array(
                "code" => 200,
                "msg" => "添加成功",
            );
        }
        $this->ajaxReturn($arr);
    }

    //修改
    public function edit(){
        $param=$_GET;
        $id=isset($param['id']) ? $param['id'] : 0;
        $title=isset($param['title']) ? $param['title'] :$this->ajaxReturn(['code'=>404,'msg'=>'title异常']);
        unset($param['id']);
        $res=M('course')->where('id',$id)->save($param);
        if (!$res) {
            $arr = array(
                "code" => 404,
                "msg" => "修改失败",
            );
        }else{
            $arr = array(
                "code" => 200,
                "msg" => "修改成功",
            );
        }
        $this->ajaxReturn($arr);
    }

    //删除
    public function del(){
        $back=[
            'code'=>404,
            'msg'=>'',
        ];
        $get = $_GET;
        $id=isset($get['id']) ? $get['id'] : '';
        $ids=isset($get['ids']) ? $get['ids'] : '';
        $del=$id!='' ? $id : $ids;
        if($del){

            $res=M('course')->where('id','in',$id)->delete();
            $back['code']=$res ? 200 : 202;
        }else{
            $back['msg']='数据异常';
        }
        $this->ajaxReturn($back);
    }
}